/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Huawei Technologies
 *
 */

#ifndef LWIP_ARCH_CC_H
#define LWIP_ARCH_CC_H

#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>

#include "lwiplog.h"

#define LWIP_NOASSERT

#define LWIP_ERRNO_STDINCLUDE 1
#define MEMP_MEMORY_BASE_PLACEHOLDER 0
#define MEMZONE_NAMESIZE 32

#define LWIP_RAND()     ((uint32_t)rand())

extern uint8_t *sys_hugepage_malloc(const char *name, uint32_t size);

#define LWIP_DECLARE_MEMP_BASE_ALIGNED(name, __size)\
PER_THREAD uint8_t *memp_memory_##name##_base; \
void alloc_memp_##name##_base(void) \
{ \
    memp_ ## name.desc = memp_desc_ ## name; \
    memp_ ## name.stats = &memp_stat ## name; \
    memp_ ## name.size = memp_size ## name; \
    memp_ ## name.num = memp_num ## name; \
    memp_ ## name.tab = &memp_tab_ ## name; \
    memp_pools[MEMP_##name] = &memp_ ## name; \
    \
    char mpname[MEMZONE_NAMESIZE] = {0}; \
    snprintf(mpname, MEMZONE_NAMESIZE, "%d_%s", (int)syscall(SYS_gettid), #name); \
    memp_memory_##name##_base = \
        sys_hugepage_malloc(mpname, LWIP_MEM_ALIGN_BUFFER(__size)); \
    memp_pools[MEMP_##name]->base = memp_memory_##name##_base; \
}

#define LWIP_DECLARE_MEMORY_ALIGNED(variable_name, size) \
PER_THREAD uint8_t *variable_name; \
void alloc_memory_##variable_name(void) \
{ \
    char mpname[MEMZONE_NAMESIZE] = {0}; \
    snprintf(mpname, MEMZONE_NAMESIZE, "%d_%s", (int)syscall(SYS_gettid), #variable_name); \
    (variable_name) = \
        sys_hugepage_malloc(mpname, LWIP_MEM_ALIGN_BUFFER(size)); \
}

#endif /* LWIP_ARCH_CC_H */
